package com.File_io_.io_.reader_;

import org.junit.jupiter.api.Test;

import java.io.FileReader;
import java.io.IOException;

/**
 * @author 宋鑫宇
 * @version 1.0
 * 使用fileReader 字符流读取文件
 */
public class FileReader_ {
    public static void main(String[] args) {

    }
    //按照数组读取文件
    @Test
    public void fileReader02(){
        String filePath = "D:\\idea_WorkSpace\\myjava\\myjava\\src\\com\\File_io_\\hello.txt";
        //创建FileReader对象
        FileReader fileReader = null;
        int readLen = 0;
        char[] buf = new char[8];
        try {
            fileReader = new FileReader(filePath);
            while ((readLen = fileReader.read(buf)) != -1){
                System.out.print(new String(buf,0,readLen));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileReader != null){
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void fileReader() {
        String filePath = "D:\\idea_WorkSpace\\myjava\\myjava\\src\\com\\File_io_\\hello.txt";
        //创建FileReader对象
        FileReader fileReader = null;
        int data = ' ';
        try {
            fileReader = new FileReader(filePath);
            while ((data = fileReader.read()) != -1){
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileReader != null){
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
